#include "FWMouse.h"


FWMouse::FWMouse(FWGame* game)
{
	this->__gameObject = game;
	this->_di8 = NULL;
	this->_did8 = NULL;
}


FWMouse::~FWMouse(void)
{
}

bool FWMouse::init()
{
	HRESULT hr = DirectInput8Create(
		__gameObject->hInstance,
		DIRECTINPUT_VERSION,
		IID_IDirectInput8,
		(void**)&this->_di8,
		NULL);
	if(FAILED(hr)) return false;
	hr = this->_di8->CreateDevice(GUID_SysMouse,&this->_did8,NULL);
	if(FAILED(hr)) return false;
	hr = this->_did8->SetDataFormat(&c_dfDIMouse);
	if(FAILED(hr)) return false;
	hr = this->_did8->SetCooperativeLevel(__gameObject->hwnd,DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
	if(FAILED(hr)) return false;
	this->_did8->Acquire();
	return true;
}

void FWMouse::getState()
{
	this->_did8->GetDeviceState(sizeof(this->_mouseState),(void**)&this->_mouseState);
}

D3DXVECTOR2 FWMouse::GetMousePosition()
{
	POINT p;
	RECT r;
	GetCursorPos(&p);
	GetClientRect(__gameObject->hwnd,&r);	
	return D3DXVECTOR2(p.x - r.left,p.y);
}

DIMOUSESTATE FWMouse::GetInfo()
{
	return this->_mouseState;
}

bool FWMouse::IsButtonDown(int num)
{
	return this->_mouseState.rgbButtons[num] & 0x80;
}